package com.springboot.demo.common.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springboot.demo.common.enums.ErrorCode;
import com.springboot.demo.common.enums.Status;
import com.springboot.demo.common.result.ApiResult;
import com.springboot.demo.common.result.FrontJSONResult;

/**
 * @ClassName:GlobalExceptionHandler.java
 * @Package com.springboot.demo.common.exception
 * @Description:全局捕获异常处理类
 * @Author JuoChiu
 * @Date  2018年2月9日上午9:51:28
 * @Version
 */
@ControllerAdvice
public class GlobalExceptionHandler {
	
	private Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public FrontJSONResult errorHandler(Exception ex) {
    	log.error(ex.getMessage(),ex);
        ApiResult result = new ApiResult();
        result.setStatus(Status.FAIL);
        result.setCode(ErrorCode.SYSTEM_EXCEPTION.getCode());
        result.setDescription("系统繁忙请稍后重试");
        return FrontJSONResult.fromExecuteResult(result);
    }

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public FrontJSONResult BizErrorHandler(BusinessException ex) {
    	log.error(ex.getMessage()+"参数是："+ex.getParams(),ex);
        ApiResult result = new ApiResult();
        result.setStatus(Status.FAIL);
        result.setCode(ex.getCode());
        result.setDescription(ex.getMessage());
        return FrontJSONResult.fromExecuteResult(result);
    }
}
